package com.pan.service.impl;

import com.pan.dao.DepositoryDao;
import com.pan.dao.DepositorySelectDao;
import com.pan.entity.Depository;
import com.pan.entity.Rc_district;
import com.pan.entity.Result;
import com.pan.enume.ResultEnum;
import com.pan.service.DepositoryService;
import com.pan.service.DistrictService;
import com.pan.tools.RedisTools;
import com.pan.vo.DepositoryCreateVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service("depositoryService")
public class DepositoryServiceImpl implements DepositoryService {

    @Resource
    private RedisTools redisTools;

    @Resource
    private DepositorySelectDao depositorySelectDao;

    @Resource
    private DistrictService districtService;

    @Resource
    private DepositoryDao depositoryDao;
    @Override
    public Result<String> getAllAddress(Integer id){
        Depository depository = depositorySelectDao.selectById(id);
        if (depository==null){
            return new Result<>(ResultEnum.NOT_FOUND_ERROR);
        }
        String allAddressName = depository.getAllAddress();
        Result result = new Result(ResultEnum.SUCCESS,allAddressName);
        return result;
    }
    @Override
    public Result<List<Depository>> selectByAddress(Integer districtId,Integer level){
        if (level>3||level<0){
            return new Result<>(403,"查询地区等级错误");
        }
        String selectMethodName = "selectByDistrict"+level+"Id";
        Class clazz = depositorySelectDao.getClass();
        try {
            Method selectMethod = clazz.getMethod(selectMethodName,Integer.class);
            List<Depository> resultList = (List<Depository>) selectMethod.invoke(districtId);
            Result result = new Result(ResultEnum.SUCCESS,resultList);
            return result;
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
    @Override
    public Result<Depository> selectById(Integer id){

        Depository depository = depositorySelectDao.selectById(id);
        if (depository==null){
            return new Result<>(ResultEnum.NOT_FOUND_ERROR);
        }
        return new Result<>(ResultEnum.SUCCESS,depository);
    }
    @Override
    @Transactional
    public Result insert(String adminTicket, DepositoryCreateVo depositoryCreateVo){
        Integer district3Id = depositoryCreateVo.getDistrict3Id();
        Result<List<Rc_district>> resultAllDistrict = districtService.getAllParentDistrict(district3Id.shortValue());
        List<Rc_district> listDistrict = resultAllDistrict.getData();
        listDistrict = listDistrict.stream()
                .sorted(Comparator.comparing(Rc_district::getLevel)).skip(1)
                .collect(Collectors.toList());
        Depository insertDepository = new Depository();
        insertDepository.setAddress(depositoryCreateVo.getAddress());
        insertDepository.setName(depositoryCreateVo.getName());
        Class depositoryClass = Depository.class;
        for(int i=1;i<4;i++){
            try {
                Method idMethod = depositoryClass.getMethod("setDistrict"+i+"Id",Integer.class);
                Method nameMethod = depositoryClass.getMethod("setDistrict"+i+"Name", String.class);
                Rc_district rc_district = listDistrict.get(i-1);
                idMethod.invoke(insertDepository,(int)rc_district.getDistrict_id());
                nameMethod.invoke(insertDepository,rc_district.getDistrict());
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
        Integer id = depositoryDao.insert(insertDepository);
        Result result = new Result(ResultEnum.SUCCESS,id);
        return result;
    }
}
